<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/5/31
 * Time: 21:11
 */
namespace app\adminapi\controller;

use app\common\controller\AdminApi;
use think\Exception;

class Config extends AdminApi
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];

    public function update()
    {
        $postData = json_decode(request()->getContent(), true);

        $configList = [];
        foreach (\app\common\model\Config::all() as $v) {
            if (isset($postData[$v['name']])) {
                $value = $postData[$v['name']];
                if (is_array($value) && isset($value['field'])) {
                    $value = json_encode(\app\common\model\Config::getArrayData($value), JSON_UNESCAPED_UNICODE);
                } else {
                    $value = is_array($value) ? implode(',', $value) : $value;
                }
                $v['value'] = $value;
                $configList[] = $v->toArray();
            }
        }

        try {
            $model = new \app\common\model\Config();
            $model->allowField(true)->saveAll($configList);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        try {
            \app\common\model\Config::refreshFile_1();
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('修改成功');

    }
}